<template>
  <Dialog v-model="dialogVisible" title="选择商品" width="80%">
    <el-form ref="queryFormRef" :model="queryParams" :inline="true">
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商品名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="商品分类" prop="categoryId">
        <el-cascader
          v-model="queryParams.categoryId"
          :options="categoryOptions"
          :props="{
            value: 'id',
            label: 'name',
            emitPath: false
          }"
          clearable
          placeholder="请选择商品分类"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      @selection-change="handleSelectionChange"
      class="mb-4"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="商品图片" align="center" width="100">
        <template #default="{ row }">
          <el-image
            :src="row.picUrl"
            class="h-40px w-40px rounded"
            :preview-src-list="[row.picUrl]"
            preview-teleported
          />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" prop="name" min-width="200" show-overflow-tooltip />
      <el-table-column label="商品分类" prop="categoryName" min-width="120" show-overflow-tooltip />
      <el-table-column label="商品价格" prop="price" min-width="100">
        <template #default="{ row }"> 
                ￥{{ fenToYuan(row.price) }}
              </template>
              </el-table-column>
      <el-table-column label="商品库存" prop="stock" min-width="100" />
      <el-table-column label="商品状态" prop="status" min-width="100">
        <template #default="{ row }">
          <el-tag :type="row.status === 1 ? 'success' : 'danger'">
            {{ row.status === 1 ? '上架' : '下架' }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <div class="flex justify-between items-center">
      <Pagination
        v-model:total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
      <div class="flex gap-2">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm" :disabled="!selectedSpus.length">
          确 定
        </el-button>
      </div>
    </div>
  </Dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import * as ProductSpuApi from '@/api/mall/product/spu'
import * as ProductCategoryApi from '@/api/mall/product/category'
import { fenToYuan } from '@/utils'

defineOptions({ name: 'SpuSelectDialog' })

const props = defineProps<{
  isSelectSku?: boolean
}>()

const emit = defineEmits(['confirm'])

const dialogVisible = ref(false)
const loading = ref(false)
const list = ref([])
const total = ref(0)
const selectedSpus = ref<any[]>([])
const categoryOptions = ref([])

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: '',
  categoryId: undefined,
  tabType: 0,
})

const queryFormRef = ref()

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProductSpuApi.getSpuPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 选择项变化 */
const handleSelectionChange = (selection: any[]) => {
  selectedSpus.value = selection
}

/** 确认选择 */
const handleConfirm = () => {
  emit('confirm', selectedSpus.value.map(item => item.id))
  dialogVisible.value = false
}

/** 打开弹窗 */
const open = () => {
  dialogVisible.value = true
  getList()
  // 加载商品分类
  if (categoryOptions.value.length === 0) {
    ProductCategoryApi.getCategoryList({}).then(data => {
      categoryOptions.value = data
    })
  }
}

defineExpose({ open })
</script>

<style scoped>
.el-cascader {
  width: 240px;
}

:deep(.el-dialog__footer) {
  padding: 0;
  margin-top: 16px;
}
</style> 